package com.yunyi.user.service.impl;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.yunyi.model.announcement.Announcement;
import com.yunyi.model.announcement.SysNotice;
import com.yunyi.model.announcement.dto.AnnouncementDto;
import com.yunyi.model.mappers.announcement.AnnouncementMapper;
import com.yunyi.response.CommonCode;
import com.yunyi.user.constant.Constant;
import com.yunyi.user.response.AnnouncementResponse;
import com.yunyi.user.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * @ClassName AnnouncementServiceImpl
 * @Description 公告信息查询
 * @Author leizhang
 * Date 2021/8/27 10:25 下午
 * @Version 1.0
 **/
@Service
public class AnnouncementServiceImpl implements AnnouncementService {
    @Autowired
    private AnnouncementMapper announcementMapper;
    @Value("${pic.url}")
    private String picUrl;

    /**
     * @param sysNotice
     * @return java.util.List<com.yunyi.model.announcement.SysNotice>
     * @Description : 公告信息查询
     * @Author : leizhang
     * @Date 10:37 下午 2021/8/27
     **/
    @Override
    public AnnouncementResponse getSysNotice(SysNotice sysNotice) {
        List<SysNotice> sysNoticeInfo = announcementMapper.getSysNoticeInfo(sysNotice);
        if (!sysNoticeInfo.isEmpty()) {
            Map<String, List<SysNotice>> collect = sysNoticeInfo.stream().collect(Collectors.groupingBy(SysNotice::getNoticeType));
            //1 通知 2 公告
            //封装返回值
            AnnouncementDto dto = new AnnouncementDto();
            List<Announcement> announcementList = Lists.newArrayList();

            List<SysNotice> sysNotices_2 = null;
            Announcement announcement = null;
            if (null != collect.get(Constant.NUMBER_2_STR)) {
                sysNotices_2 = collect.get(Constant.NUMBER_2_STR).stream().sorted(Comparator.comparing(SysNotice::getCreateTime).reversed()).collect(Collectors.toList());
                for (SysNotice notice : sysNotices_2) {
                    String imageUrl = notice.getImageUrl();
                    announcement = new Announcement();
                    announcement.setNotice(notice.getNoticeTitle());
                    //拼接图片访问路径
                    announcement.setSmallImg(picUrl.concat(imageUrl.substring(imageUrl.indexOf("/profile") + 9)));
                    announcement.setNoteText(notice.getNoticeContent());
                    announcementList.add(announcement);
                }
            }
            List<SysNotice> sysNotices_1 = null;
            if (null != collect.get(Constant.NUMBER_1_STR)) {
                sysNotices_1 = collect.get(Constant.NUMBER_1_STR).stream().sorted(Comparator.comparing(SysNotice::getCreateTime).reversed()).collect(Collectors.toList());
                dto.setNoteText(sysNotices_1.get(Constant.NUMBER_0).getNoticeContent());
            }
            //通知
            dto.setNoticeData(announcementList);
            return new AnnouncementResponse(CommonCode.SUCCESS, dto);
        }
        return null;
    }
}
